<?php
	session_start();
	require_once('common/config.php');
	require_once('../_helper/paging.php');
	require_once('../_helper/helper.php');
	require_once('../_model/album_model.php');
	
	
	$pageCMS = PAGE_CMS_ALBUM;
	$keyword = '';
	$type_id = '';
	$brand_id = '';
	
	if(!isset($_SESSION['logon']))
	{
		header("Location: /wks/login.php?url=product");
		exit();
	}
	
	if (!isset($_GET['action']))
		$_GET['action'] = "list";
		
	if (isset($_GET['id'])){
		$album_id = $_GET['id'];
		}
	if (isset($_GET['keyword'])){
		$keyword = $_GET['keyword'];
	}
	if (isset($_GET['brand_id'])){
		$brand_id = $_GET['brand_id'];
	}
	if (isset($_GET['type_id'])){
		$type_id = $_GET['type_id'];
	}
	
	$action = $_GET['action'];
	
	if($action == 'list' || $action =='search')
	{
		if(isset($_GET['page']))
			$curPage = $_GET['page'];
		else
		{
			$curPage = 0;
			unset($_SESSION['numAlbum']);
		}
			
		
		/*var_dump($type_arr);die();*/
		if( !isset($_SESSION['numAlbum']))
		{
			$numAlbum = GetNumAlbumSearch($type_id, $brand_id,$keyword);
			$_SESSION['numAlbum'] = $numAlbum;
		}
		else
			$numAlbum = $_SESSION['numAlbum'];
			
		$param = "action=search&keyword={$keyword}";
		
		$album_arr = GetAlbumSearch($curPage, PAGING_NUM_ROW, $keyword);
		
				
		//Xu ly
		require_once('template/album/album_list.php');
		exit();
	}
	
	if($action == 'add')
	{
		if(!isset($_POST['submit']))
		{
			require_once('template/album/album_add.php');
			exit();
		}
		else
		{
			//var_dump($_POST); die();
			if($_POST['album_name'] != '')
			{
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['album_description'] = str_replace($order,'',$_POST['album_description']);
				
				$result = InsertAlbum($_POST);
				if($result)
				{
					//$url = urlencode(BASE_URL. "/sitemap.xml");
					//$url = 'http://www.google.com/webmasters/tools/ping?sitemap='.$url;
					//GetContent($url);
					header("Location: /wks/album.php?action=list");
				}
				else
					header("Location: /wks/album.php?action=list&error=1&e_action=add");
			}
			else
				header("Location: /wks/album.php?action=list&error=1&e_action=add");
		}
		exit();
	}
	
	if($action == 'edit')
	{
		if (!isset($album_id))
			header("Location: /wks/album.php");		
			
		if(!isset($_POST['submit'])){
			$album_value = GetAlbumById($album_id);
			
			require_once('template/album/album_edit.php');
			exit();
		}
		else{
			if($_POST['album_name'] != '')
			{
				$order   = array("\r\n", "\n", "\r", "\t", "  ");
				$_POST['album_description'] = str_replace($order,'',$_POST['album_description']);
				
				UpdateAlbum($album_id, $_POST);
				header("Location: /wks/album.php?action=list");
			}
		}
		exit();
	}
	
	if($action == 'del')
	{
		$result = FALSE;
		if (isset($album_id))
			$result = DeleteAlbum($album_id);
			
		if($result == FALSE)
			header("Location: /wks/album.php?error=1&e_action=del");	
		else
		{
			header("Location: /wks/album.php");	
		}
	}
	if($action == 'adjorder')
	{
		//var_dump($_GET); die();
		$album_id = $_GET['id'];
		
		if($_GET['adj'] == 'inc')
		{
			if (UpdateAlbumOrder($album_id, '+') )
				echo "ok";
			else
				echo "fail";
		}
		if($_GET['adj'] == 'desc')
		{
			if (UpdateAlbumOrder($album_id, '-') )
				echo "ok";
			else
				echo "fail";
		}
		exit();
	}
	if($action == 'adjnew')
	{
		$album_id = $_GET['id'];
		
		if($_GET['adj'] == 'new')
		{
			if (UpdateAlbumNew($album_id, 'new') )
				echo "ok";
			else
				echo "fail";
		}
		if($_GET['adj'] == 'old')
		{
			if (UpdateAlbumNew($album_id, 'old') )
				echo "ok";
			else
				echo "fail";
		}
		exit();
	}
?>